package project.interactive_face;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import project.function.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class Interactive_action extends Interactive_login implements Interactive_webcam{
    @Override
    public void init() {
        super.init();

        //登录按钮功能
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //getpassword方法返回的字符串数组经过自动加密，就算不输入也不会返回空
                String ID = user.getText();
                String pasw = String.valueOf(password.getPassword());
                if(ID.equals("")&&!pasw.equals("")){
                    JOptionPane.showMessageDialog(null,
                            "账号不能为空！","账号为空",JOptionPane.ERROR_MESSAGE);
                    //清除密码框中的信息
                    password.setText("");
                    //清除账号框中的信息
                    user.setText("");
                }
                else if(!ID.equals("")&&pasw.equals("")){
                    JOptionPane.showMessageDialog(null,
                            "密码不能为空！","密码为空",JOptionPane.ERROR_MESSAGE);
                    //清除密码框中的信息
                    password.setText("");
                    //清除账号框中的信息
                    user.setText("");
                }
                else if(ID.equals("")&&pasw.equals("")){
                    JOptionPane.showMessageDialog(null,
                            "账号密码不能为空！","账号密码为空",JOptionPane.ERROR_MESSAGE);
                }
                else{
                    //创建一个Admin用户，把输入框中的用户名密码和提出来
                    Admin admin = new Admin();
                    admin.setID(ID);
                    admin.setPassword(pasw);
                    //登录
                    Login login = new Login();
                    login.setAdmin(admin);
                    if(login.JudgeAdmin()==0) {
                        //弹出账号或密码错误的窗口
                        JOptionPane.showMessageDialog(null, "账号或密码错误", "账号或密码错误", JOptionPane.WARNING_MESSAGE);
                        //清除密码框中的信息
                        password.setText("");
                        //清除账号框中的信息
                        user.setText("");
                    } else {
                        //弹出登录成功的窗口
                        jf.dispose();
                        JOptionPane.showMessageDialog(null,"欢迎您："+ID);

                    }
                }
            }
        });

        //人脸识别登录
        shoot.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "将进行人脸识别", "人脸识别", JOptionPane.NO_OPTION);
                iwo.jd.setVisible(true);
            }
        });

        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开注册窗口
                ir.jd2.setVisible(true);
            }
        });

        ir.registerOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String ID = ir.user.getText();
                String passwd = new String (ir.new_password1.getPassword());
                String confrimpasswd = new String (ir.new_password2.getPassword());

                //创建Register类
                Register register = new Register();
                register.setID(ID);
                register.setPassword(passwd);
                register.setconfirmpasswd(confrimpasswd);

                //如果注册成功，返回登录界面
                try {
                    if(register.JudgeRegister()) {
                        JOptionPane.showMessageDialog(null,"注册完成，接下来录入人脸");
                        iwa.jd.setVisible(true);
                    }
                    else{
                        ir.user.setText("");
                        ir.new_password2.setText("");
                        ir.new_password1.setText("");
                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    //e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        iwo.open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(iwo.open.getText().equals("打开摄像头")){
                    iwo.open.setText("关闭摄像头");
                    iwo.wp1.setVisible(true);
                }
                else{
                    iwo.open.setText("打开摄像头");
                    iwo.wp1.setVisible(false);
                }
            }
        });

        iwa.open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(iwa.open.getText().equals("打开摄像头")){
                    iwa.open.setText("关闭摄像头");
                    iwa.wp2.setVisible(true);
                }
                else{
                    iwa.open.setText("打开摄像头");
                    iwa.wp2.setVisible(false);
                }
            }
        });

        //人脸识别
        iwo.shoot.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(iwo.open.getText().equals("打开摄像头")) {
                    JOptionPane.showMessageDialog(null, "摄像头未打开，请打开摄像头");
                }
                else{
                    Function_Webcam.pick();
                    JSONObject res = Function_Webcam.detect();
                    if(res.get("error_msg").equals("SUCCESS")){
                        iwo.jd.dispose();
                    }
                    else JOptionPane.showMessageDialog(null,"人脸检测失败，请重试");
                    JSONObject res1 = Function_Webcam.search();
                    System.out.println(res1);
                    if(res1.get("error_msg").equals("match user is not found")){
                        JOptionPane.showMessageDialog(null,"未找到该用户","null",JOptionPane.ERROR_MESSAGE);
                    }
                    else if(res1.get("error_msg").equals("SUCCESS")){
                        JOptionPane.showMessageDialog(null,"登陆成功！");
                        jf.dispose();
                    }else{
                        JOptionPane.showMessageDialog(null,"请重新拍摄","repick",JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });

        iwa.append.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(iwa.open.getText().equals("打开摄像头")) {
                    JOptionPane.showMessageDialog(null, "摄像头未打开，请打开摄像头");
                }
                else{
                    Function_Webcam.pick();
                    String ID = ir.user.getText();
                    ir.jd2.dispose();
                    JSONObject res = Function_Webcam.addUser(ID);
                    System.out.println(res.get("error_msg"));
                    if(res.get("error_msg").equals("SUCCESS")){
                        JOptionPane.showMessageDialog(null,"人脸添加完成");
                        iwa.jd.dispose();
                    }else{
                        JOptionPane.showMessageDialog(null,"请重试人脸录入","失败",JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
    }
}
